package com.daiyang.shixun.dao;

import com.daiyang.shixun.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    public User login(Connection con, User user) throws SQLException {
        //jdbc编程
        User resulitUser = null;
        // 编写SQL语句,查询用户是否存在
        String sql = "SELECT * FROM t_user WHERE username = ? AND password = ?";

        // 预编译SQL语句,传入sql语句
        PreparedStatement preparedStatement = con.prepareStatement(sql);

        // 传入用户名
        preparedStatement.setString(1, user.getUserName());
        // 传入密码
        preparedStatement.setString(2, user.getPassword());
        // 执行SQL语句,查询结果集
        ResultSet resultSet = preparedStatement.executeQuery();

        // 处理结果集,如果查询到用户,则返回User对象,否则返回null
        if (resultSet.next()) {
            resulitUser = new User();
            resulitUser.setUserName(resultSet.getString("userName"));
            resulitUser.setPassword(resultSet.getString("password"));
        }
        return resulitUser;
    }
}
